﻿using FarmSmartSleep.Common.Domain.PM;
using FarmSmartSleep.Common.Dtos;
using FarmSmartSleep.Common.Infrastructure;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace FarmSmartSleep.Organ.API.Application.ProductionManagement.MotherGivingBirthFile
{
    /// <summary>
    /// 母猪出生删除
    /// </summary>
    public class MotherGivingBirthDeleteHandler:IRequestHandler<MotherGivingBirthDeleteCommon,Result>
    {
        private readonly IBaseRepository<MotherGivingBirth> motherGivingBirthRep;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="motherGivingBirthRep"></param>
        public MotherGivingBirthDeleteHandler(IBaseRepository<MotherGivingBirth> motherGivingBirthRep)
        {
            this.motherGivingBirthRep = motherGivingBirthRep;
        }

        public async Task<Result> Handle(MotherGivingBirthDeleteCommon request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CodeEnum.delete};
            try
            {
                var n = 0;
                var list = await motherGivingBirthRep.GetAll().Where(c => request.motherGivingBirthIds.Contains(c.MotherGivingBirthId)).ToListAsync();
                foreach (var item in list)
                {
                    item.Deletes = true;
                    item.DeleteTime = DateTime.Now;
                    item.DeleteId = item.CreateId;
                  n+=  motherGivingBirthRep.Update(item);
                }
                if(n!=0)
                {
                    result.Code = (int)CodeEnum.success;
                    result.Messege = "删除成功";
                }
               
                 

            }
            catch (Exception)
            {
                result.Messege = "删除失败";
                throw;
            }
            return result;
        }
    }
}
